//
//  JKSerialPort.h
//  MyFramework
//
//  Created by Jack.MT on 15/7/20.
//  Copyright (c) 2015年 Jack.MT. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <termios.h>
#import <sys/ioctl.h>

#define SERIALPORT_MAX_BUFFER_SIZE     8196UL

/** Bardrate */
typedef enum
{
    JKBR_50 = B50,
    JKBR_75 = B75,
    JKBR_110 = B110,
    JKBR_134 = B134,
    JKBR_150 = B150,
    JKBR_300 = B300,
    JKBR_600 = B600,
    JKBR_1200 = B1200,
    JKBR_1800 = B1800,
    JKBR_2400 = B2400,
    JKBR_4800 = B4800,
    JKBR_9600 = B9600,
    JKBR_19200 = B19200,
    JKBR_38400 = B38400,
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    JKBR_7200 = B7200,
    JKBR_14400 = B14400,
    JKBR_28800 = B28800,
    JKBR_57600 = B57600,
    JKBR_76800 = B76800,
    JKBR_115200 = B115200,
    JKBR_230400 = B230400,
#endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
}JKBaudRate;

/** Databits */
typedef enum
{
    JKDB_5 = CS5,
    JKDB_6 = CS6,
    JKDB_7 = CS7,
    JKDB_8 = CS8,
}JKDataBit;

/** Parity */
typedef enum
{
    JKPar_NONE = 0,
    JKPar_ODD = PARENB & PARODD,
    JKPar_EVEN = JKPar_ODD & ~PARODD
}JKParity;
#define JKPARITY_MARK (PARENB & PARODD);

/** flow control of io */
typedef enum
{
    JKFC_HARDWARE,
    JKFC_SOFTWARE,
    JKFC_NONE
}JKFlowControl;

typedef enum
{
    JKSB_ONE = 0,
    JKSB_TWO = CSTOPB
}JKStopBit;

/* Serial Port class created by Jack.Zhao */
@interface JKSerialPort : NSObject
{
    int mFileDesriptor;
    
    NSString *mDevicePath;
    JKBaudRate mBaudRate;
    JKDataBit mDataBit;
    JKStopBit mStopBit;
    JKParity mParity;
    JKFlowControl mFlowControl;
    
    char mBuffer[SERIALPORT_MAX_BUFFER_SIZE];
}

/** path in pc system to specify the device */
@property NSString *devicePath;
/** baud rate to communicate with device */
@property JKBaudRate baudrate;
@property JKDataBit dataBit;
@property JKStopBit stopBit;
@property JKParity parity;
@property JKFlowControl flowControl;
@property(readonly) BOOL opened;

-(instancetype)initWithDevicePath:(NSString *) devPath;
-(instancetype)initWithDevicePath:(NSString *)devPath andBaudRate:(JKBaudRate)baudRate;
-(instancetype)initWithDevicePath:(NSString *)devPath andBaudRate:(JKBaudRate)baudRate andDataBit:(JKDataBit) dataBit andStopBit:(JKStopBit)stopBit andParity:(JKParity)parity;

-(BOOL)open;
-(BOOL)openWithDevicePath:(NSString *)devPath;
-(BOOL)openWithDevicePath:(NSString *)devPath andBaudRate:(JKBaudRate)baudRate andDataBit:(JKDataBit) dataBit andStopBit:(JKStopBit)stopBit andParity:(JKParity)parity;

-(void)close;

-(NSString *)readExsiting;
-(NSString *)readExsitingWithEncoding:(NSStringEncoding)encoding;
-(NSString *)readLine;
-(NSString *)readLineWithEncoding:(NSStringEncoding) encoding;
-(NSString *)readToChar:(char) c;
-(NSString *)readToChar:(char) c withEncoding:(NSStringEncoding) encoding;
-(NSString *)readToString:(NSString *) endStr;
-(NSString *)readToString:(NSString *) endStr withEncoding:(NSStringEncoding) encoding;
-(int)readBytesToArray:(Byte *)bytes withCount:(int) count;
-(int)readNewestBytesToArray:(Byte *)bytes withCount:(int) count;

-(void)writeString:(NSString *)strDataToSend;
-(void)writeString:(NSString *)strDataToSend withEncoding:(NSStringEncoding)encoding;
-(void)writeData:(NSData *)data;
-(void)writeBytes:(Byte *)bytes ofCount:(unsigned long) count;

-(NSString *)writeAndReadString:(NSString *) strDataToSend withInterval:(double) intervalInSeconds;
-(NSString *)writeAndReadString:(NSString *) strDataToSend withEncoding:(NSStringEncoding) encoding withInterval:(double) intervalInSeconds;
-(NSData *)writeAndReadBytes:(Byte *)bytes ofCount:(int)count withInterval:(double) intervalInSeconds;


+(NSArray *)portPaths;

@end
